package com.dxq.wan;

import android.app.Application;

import com.dxq.basic_ui_library.loadSirCallBack.CustomCallback;
import com.dxq.basic_ui_library.loadSirCallBack.EmptyCallback;
import com.dxq.basic_ui_library.loadSirCallBack.ErrorCallback;
import com.dxq.basic_ui_library.loadSirCallBack.LoadingCallback;
import com.dxq.basic_ui_library.loadSirCallBack.TimeoutCallback;
import com.dxq.wan.api.Api;
import com.dxq.wan.api.ApiService;
import com.kingja.loadsir.core.LoadSir;
import com.zhouyou.http.EasyHttp;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author zsh
 * @date 2020/3/10.
 * 功能：
 */

public class App extends Application {

    public static ApiService apiService;

    @Override
    public void onCreate() {
        super.onCreate();
        initLoadSir();
        initEasyHttp();
        initRetrofitHttp();
    }

    private void initRetrofitHttp() {
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(Api.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        apiService = retrofit.create(ApiService.class);
    }

    private void initEasyHttp() {
        EasyHttp.init(this);//默认初始化
        EasyHttp.getInstance()
                //可以全局统一设置全局URL
                .setBaseUrl(Api.baseUrl)//设置全局URL  url只能是域名 或者域名+端口号
                .addConverterFactory(GsonConverterFactory.create());
    }

    private void initLoadSir() {
        LoadSir.beginBuilder()
                .addCallback(new ErrorCallback())
                .addCallback(new EmptyCallback())
                .addCallback(new LoadingCallback())
                .addCallback(new TimeoutCallback())
                .addCallback(new CustomCallback())
                .setDefaultCallback(LoadingCallback.class)
                .commit();
    }
}
